package com.ruoyi.common.utils;

/**
 * @author mac
 */
public class PhoneCompressUtil {

    private static char[] bit62 = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};


    public static void main(String[] args) {
        String telephoneNumberStr = "01889188989";
        String abc = digit10to16(telephoneNumberStr);
        System.out.println("十进制数转换成六十二进制数结尾为：" + abc);
        System.out.println("六十二进制数转换成十进制数结尾为：" + digit16to10(abc));
    }

    /**
     * 十进制数(18957124766)转换成六十二进制数(kGWa8S)
     * 18957124766除以62等于305760076;  18957124766除以62取余54
     * 305760076除以62等于4931614;  305760076除以62取余8
     * 4931614除以62等于79542;  4931614除以62取余10
     * 79542除以62等于1282;  79542除以62取余58
     * 1282除以62等于20;  1282除以62取余42
     * 20除以62等于0;  20除以62取余20
     *
     * @param telephoneNumberStr
     * @return
     */
    public static String digit10to16(String telephoneNumberStr) {

        long source10digit = Long.parseLong(telephoneNumberStr);
        StringBuffer sb = new StringBuffer();
        long b = divisionBy62(source10digit);
        int a = modBy62(source10digit);
        while (b > 0) {
            sb.append(bit62[a]);
            a = modBy62(b);
            b = divisionBy62(b);
        }
        sb.append(bit62[a]);
        String abc = sb.reverse().toString();
        return abc;
    }

    /**
     * 对十进制数除以62
     *
     * @param dividend
     * @return
     */
    private static long divisionBy62(long dividend) {
        return dividend / 62;
    }

    /**
     * 对十进制数除以62取余
     *
     * @param dividend
     * @return
     */
    private static int modBy62(long dividend) {
        return (int) (dividend % 62);
    }

    /**
     * 获取六十二进制数字符在该进制规范中的顺序编号
     *
     * @param a
     * @return
     */
    private static int digit16by10(char a) {
        for (int i = 0; i < bit62.length; i++) {
            if (a == bit62[i]) {
                return i;
            }
        }
        return 0;
    }


    /**
     * 六十二进制数(kGWa8S)转换成十进制数结(18957124766)
     * 20(k)*62^5=18322656640
     * 42(G)*62^4=620606112
     * 58(W)*62^3=13823024
     * 10(a)*62^2=38440
     * 8(8)*62^1=496
     * 54(S)*62^0=54
     *
     * @param str
     * @return
     */
    public static String digit16to10(String str) {
        if (str == null || "".equals(str.trim())) {
            return "0";
        }
        long a = 0;
        int strLen = str.length();
        for (int i = 0; i < strLen; i++) {
            a = a + digit16by10((char) str.charAt(i)) * (long) Math.pow(62, strLen - i - 1);
        }
        String aStr = String.valueOf(a);
        //补0
        if (aStr.length() == 10 && !aStr.startsWith("0")) {
            aStr = "0" + aStr;
        }
        return aStr;
    }
}
